package org.iteam.xlz.manager.action.sys;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.commons.security.sha.ShaUtil;
import org.iteam.core.Constant;
import org.iteam.core.Constant.Status;
import org.iteam.core.model.sys.SysUser;
import org.iteam.core.service.sys.SysUserService;
import org.iteam.core.shiro.AuthUtils;
import org.iteam.core.view.sys.AuthView;
import org.iteam.web.struts.action.AbstractModuleAction;
import org.iteam.web.struts.action.RestAction;

import com.opensymphony.xwork2.ModelDriven;

/**
 * 系统用户管理
 */
@SuppressWarnings("unchecked")
@Results({
		@Result(type = "freemarker", name = "INDEX", location = "/WEB-INF/content/sys/user-index.html"),
		@Result(type = "freemarker", name = "EDIT", location = "/WEB-INF/content/sys/user-edit.html") })
public class SysUserAction extends AbstractModuleAction<SysUser> implements RestAction, ModelDriven<SysUser> {

	/**
	 * 列表页
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(PATH + "/index")
	public String index() {
		map.put("status", Status.values());
		return INDEX;
	}

	/**
	 * 客户信息管理列表查询
	 * 
	 * @return
	 */
	@Action(PATH + "/query")
	public String query() {
		try {
			map.put("total", service.count());
			map.put("rows", service.queryByPage(page, rows));
		} catch (Exception e) {
			e.printStackTrace();
			map.put("total", 0);
			map.put("rows", new ArrayList<SysUser>());
		}
		return MAP;
	}

	/**
	 * 帐号效验
	 */
	@Action(PATH + "/validate/account")
	public void validateAccount() {
		try {
			print(service.validate(model.getAccount()) == 0);
		} catch (SQLException e) {
			e.printStackTrace();
			print(false);
		}
	}

	/**
	 * 查看单条记录
	 * 
	 * @return
	 */
	@Action(PATH + "/{_id}")
	public String show() {
		return null;
	}

	/**
	 * 创建记录
	 * 
	 * @return
	 */
	@Action(PATH + "/create")
	public String create() {
		try {
			model.setPassword(ShaUtil.toSha256String(ShaUtil.toSha256String(Constant.DEFAULT_PWD)));
			map.put("state", service.create(model));
		} catch (Exception e) {
			map.put("state", false);
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 更新记录
	 * 
	 * @return
	 */
	@Action(PATH + "/update")
	public String update() {
		try {
			map.put("state", service.update(model));
		} catch (SQLException e) {
			map.put("state", false);
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 获取记录
	 * 
	 * @return
	 */
	@Action(PATH + "/{_id}/sub")
	public String querySub() {
		try {
			List<AuthView> permission = service.querySub(_id);
			map.put("rows", permission);
			map.put("total", permission.size());
		} catch (Exception e) {
			map.put("module", new ArrayList<AuthView>());
			map.put("total", 0);
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 用户角色授权
	 * 
	 * @return
	 */

	@Action(PATH + "/{_id}/role")
	public String updateUserRole() {
		try {
			map.put("state", service.updateUserRole(_id, _module, _permission));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 刷新权限
	 * 
	 * @return
	 */
	@Action(PATH + "/refurbish")
	public String refurbish() {
		AuthUtils.load();
		AuthUtils.loadPermissions();
		map.put("state", true);
		return MAP;
	}

	/**
	 * 默认角色设置
	 * 
	 * @return
	 */
	@Action(PATH + "/{_id}/roleDefault")
	public String updateUserRoleDefault() {
		try {
			map.put("state", service.updateUserRoleDefault(_id, _module));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 删除记录
	 * 
	 * @return
	 */
	@Action(PATH + "/{_id}/delete")
	public String delete() {
		try {
			map.put("state", service.delete(_id));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 重置
	 * 
	 * @return
	 */
	@Action(PATH + "/{_id}/reset")
	public String reset() {
		try {
			map.put("state", service.reset(_id, ShaUtil.toSha256String(ShaUtil.toSha256String(Constant.DEFAULT_PWD))));
		} catch (Exception e) {
			map.put("state", false);
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 销毁记录
	 * 
	 * @return
	 */
	@Action(PATH + "/{_id}/destroy")
	public String destroy() {
		try {
			map.put("state", service.destroy(_id));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 编辑记录
	 * 
	 * @return
	 */

	@Action(PATH + "/{_id}/edit")
	public String edit() {
		try {
			object = service.show(_id);
			map.put("status", Status.values());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return EDIT;
	}

	/**
	 * 新增记录
	 * 
	 * @return
	 */
	@Action(PATH + "/new")
	public String editNew(){
		object = model;
		map.put("status", Status.values());
		return EDIT;
	}

	public SysUser getModel() {
		if (model == null)
			model = new SysUser();
		return model;
	}

	private static final long serialVersionUID = -5800011834083416221L;
	private SysUserService service = SysUserService.getInstance();
	private boolean _permission;
	private String _module;
	private final String PATH = "/sys/user";

	public boolean is_permission() {
		return _permission;
	}

	public void set_permission(boolean _permission) {
		this._permission = _permission;
	}

	public String get_module() {
		return _module;
	}

	public void set_module(String _module) {
		this._module = _module;
	}
}
